package sdk.webview.fmc.com.fmcsdk.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import sdk.webview.fmc.com.fmcsdk.R;


/**
 * 可输入的dialog，用于修改密码和邮箱
 */
public class EditTextDialog extends Dialog {
	private Context context;
	private String title;
	private String content;
	private ClickListenerInterface clickListenerInterface;
	private EditText et_text;

	public EditTextDialog(Context context, String title,
                          String content) {
		super(context, R.style.Dialog);
		this.context = context;
		this.title = title;
		this.content = content;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edittext_dialog);
		TextView tv_title = (TextView) findViewById(R.id.tv_title);
		et_text = (EditText) findViewById(R.id.et_text);
		TextView confirm = (TextView) findViewById(R.id.confirm);
		TextView cancel = (TextView) findViewById(R.id.cancel);
		tv_title.setText(title);
		et_text.setHint(content);
		confirm.setOnClickListener(new clickListener());
		cancel.setOnClickListener(new clickListener());
		//设置dialog的属性
		Window dialogWindow = getWindow();
	    WindowManager.LayoutParams lp = dialogWindow.getAttributes();
	    DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高
	    lp.width = (int) (d.widthPixels * 0.9);
	    dialogWindow.setAttributes(lp);

	}

	public void setClicklistener(ClickListenerInterface clickListenerInterface) {
		this.clickListenerInterface = clickListenerInterface;
	}

	private class clickListener implements View.OnClickListener {
		@Override
		public void onClick(View v) {
			int id = v.getId();
			if (id==R.id.confirm){
				clickListenerInterface.doConfirm(et_text.getText().toString().trim());
			}else if (id == R.id.cancel){
				clickListenerInterface.doCancel();
			}
			}

	};

	public interface ClickListenerInterface {

		public void doConfirm(String content);

		public void doCancel();
	}

}
